package com.itcast.springboot.waste_recovery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itcast.springboot.utils.AddressUtil;
import com.itcast.springboot.waste_recovery.domain.UserAddress;
import com.itcast.springboot.waste_recovery.mapper.UserAddressMapper;
import com.itcast.springboot.waste_recovery.service.UserAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户地址表 服务实现类
 * </p>
 *
 * @author 小破烂
 * @since 2020-08-19
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {

    @Resource
    UserAddressMapper userAddressMapper;

    @Resource
    AddressUtil addressUtil;

    @Override
    public List<UserAddress> getUserAddress(Integer id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("address_user_id" , id);
        List<UserAddress> userAddresses = userAddressMapper.selectList(wrapper);
        return userAddresses;
    }

    @Override
    public Integer updateAddressDefault(Integer userId, Integer addressId) {
        Integer result = addressUtil.setAddressDefault(userId, addressId);
        return result;
    }

    @Override
    public Integer insertAddress(UserAddress userAddress) {


        Integer result = userAddressMapper.insert(userAddress);
        if (result > 0){
            //如果要设置当前地址为默认地址
            if (userAddress.getAddressIsDefault()) {
                System.out.println(userAddress.getAddressUserId());
                System.out.println(userAddress.getAddressId());
                addressUtil.setAddressDefault(userAddress.getAddressUserId(), userAddress.getAddressId());
            }
            //返回address_id
            return userAddress.getAddressId();
        }
        return result;
    }

    @Override
    public Integer updateAddress(UserAddress userAddress) {
        int result = userAddressMapper.updateById(userAddress);
        return result;
    }

    @Override
    public Integer deleteAddress(Integer addressId) {
        int result = userAddressMapper.deleteById(addressId);
        return result;
    }
}
